<?php


namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\CompanyProfile;
use app\common\model\xilujob\PackageOrder AS PackageOrderModel;

class PackageOrder extends XilujobApi
{
    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];

    protected $restrictedActions = ["*"];

    /**
     * 订单列表
     * @return \think\response\Json
     */
    public function lists(){
        $pagesize = $this->request->param('pagesize',10);
        $state = $this->request->param('state',2);
        $where = [];
        if ($state > 0){
           $where['pay_status'] = $state;
        }
        $company = CompanyProfile::where('user_id',$this->auth->id)->find();
        $lists = PackageOrderModel::where('company_id', $company->id)->where($where)
            ->field("id,order_no,total_price,pay_price,package_name,pay_type,pay_status,paytime,updatetime,package_expires,package_unit,job_publish_num,resume_view_num,interview_num,job_refresh_num,remarks")
            ->order('updatetime','desc')
            ->paginate($pagesize);
        $this->success('',$lists);
    }

    public function detail(){
        $id = $this->request->param('order_id');
        if(!$id){
            $this->error("参数错误");
        }
        $row = PackageOrderModel::get($id);
        if(!$row){
            $this->error("订单不存在");
        }
        $this->success("",$row);
    }

    /**
     * 订单取消
     * @throws \think\exception\DbException
     */
    public function cancel(){
        $id = $this->request->post('package_order_id');
        if(!$id){
            $this->error("参数错误");
        }
        $row = PackageOrderModel::get($id);
        if(!$row){
            $this->error("订单不存在");
        }
        if($row->pay_status != 1){
            $this->error("当前状态不可取消");
        }
        $row->pay_status = 0;
        $row->canceltime = time();
        $row->save();
        $this->success("取消成功");
    }

    public function up_cert(){
        $id = $this->request->post('package_order_id');
        $cert = $this->request->post('cert');
        if(!$cert){
            $this->error('请上传凭证');
        }
        $row = $id ? PackageOrderModel::get($id) : null;
        if(!$row){
            $this->error("订单不存在");
        }
        if($row->pay_status != 1){
            $this->error("不可上传凭证");
        }
        if($row->cert_up == 1){
            $this->error("凭证不要重复上传");
        }
        $row->cert_up = 1;
        $row->cert = $cert;
        $row->save();
        $this->success("凭证上传成功");
    }

}